package com.baidu.io;

import java.io.*;

public class D07_BufferdInputStreamCopy {
    public static void main(String[] args) {
        //1.创建输入流管道
        InputStream fileInputStream = null;
        //2.将输入流管道包装成带缓冲区的输入流
        BufferedInputStream bufferdInputStream = null;
        //3.创建输出流管道
        OutputStream fileOutputStream = null;
        //4.将输出流包装成带缓冲区的输出流
        BufferedOutputStream bufferedOutputStream = null;

        try {
            fileInputStream = new FileInputStream("src/002.dat");
            bufferdInputStream = new BufferedInputStream(fileInputStream);
            fileOutputStream = new FileOutputStream("src/002_copy.dat");
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //5.创建字节数组用于单次获取字节数
            byte[] bytes = new byte[48];
            //6.创建长度用于获取某次角标
            int len = -1;
            //7.循环读取写入
            while ((len = bufferdInputStream.read(bytes)) != -1){
                bufferedOutputStream.write(bytes, 0, len);
            }
            System.out.println("复制完成");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //8.释放资源
                bufferedOutputStream.close();
                fileOutputStream.close();
                bufferdInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
